package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.me.adapter.LinkAdapter;
import com.uchengwang.ui.cm_ui.me.bean.LinkAccountBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.SwitchAccountUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2017/12/4.
 * 关联账号界面
 */

public class LinkAccountActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private RecyclerView rv;
    private LinearLayout noData;
    private LinkAdapter adapter;
    private ImageView noData_image;
    private TextView noData_text;
    private Button link_add;

    private List<LinkAccountBean> beanList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_link_account);
        setTitle("关联账号");

        initView();
    }

    private void initView(){
        rv = (RecyclerView) findViewById(R.id.link_rv);
        noData = (LinearLayout) findViewById(R.id.link_noData);
        noData_image = (ImageView) findViewById(R.id.noData_image);
        noData_text = (TextView) findViewById(R.id.noData_text);

        link_add = (Button) findViewById(R.id.link_add);
        link_add.setOnClickListener(this);

        initData();
    }

    private void initData(){
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new LinkAdapter(this);
        rv.setAdapter(adapter);

        getLinkAccounts();
    }

    /**
     * 获取关联账号信息
     * */
    private String currentToken;//当前账号token
    private void getLinkAccounts(){
        currentToken = UChengApplication.TOKEN;
        //用主账号token 获取关联数据
        String mainToken = SPUtils.getPreferences(this).getLinkMainToken();
        if (!TextUtils.isEmpty(mainToken)){
            UChengApplication.TOKEN = mainToken;
        }

        HttpMeManager.getPresenter().getLinkAccount(this, new OnHttpResultListener<LinkAccountBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<LinkAccountBean> list) {
                UChengApplication.TOKEN = currentToken;
                if (list != null && list.size() >0){
                    beanList = list;

                    //将主账号信息添加上去
//                    LinkAccountBean bean = new LinkAccountBean();
//                    bean.setMainUid(SPUtils.getPreferences(LinkAccountActivity.this).getAccountId());
//                    bean.setFakerUid(SPUtils.getPreferences(LinkAccountActivity.this).getAccountId());
//                    bean.setFakerUniId(SPUtils.getPreferences(LinkAccountActivity.this).getLong(BaseConfig.UNI_ID));
//                    bean.setFakerUniName(SPUtils.getPreferences(LinkAccountActivity.this).getString(BaseConfig.UNI_NAME));
//                    bean.setFakerNickName(SPUtils.getPreferences(LinkAccountActivity.this).getString(BaseConfig.NICK_NAME));
//                    bean.setFakerUserAvatar(SPUtils.getPreferences(LinkAccountActivity.this).getString(BaseConfig.USER_AVATAR));
//                    bean.setFakerUserRole(SPUtils.getPreferences(LinkAccountActivity.this).getInt(BaseConfig.USER_ROLE));
//                    bean.setFakerToken(SPUtils.getPreferences(LinkAccountActivity.this).getToken());
//
//                    beanList.add(0, bean);

                    adapter.setData(beanList);
                    LiteOrmDBUtil.getLiteOrm(LinkAccountActivity.this).deleteAll(LinkAccountBean.class);
                    LiteOrmDBUtil.getLiteOrm(LinkAccountActivity.this).insertAll(beanList);
                }else {
                    rv.setVisibility(View.GONE);
                    noData.setVisibility(View.VISIBLE);
                    ImageManager.getInstance().loadImage(LinkAccountActivity.this, noData_image, R.drawable.icon_000_cover, ImageManager.ImageType.NARMOL);
                    noData_text.setText("暂时没有关联账户");

                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.link_add://关联账户
                showLink();
                break;
            case R.id.linkItem_close://关闭关联的dialog
                DialogUtils.getUtils().cancel();
                break;

            case R.id.linkItem_show://显示密码
                if (isShow){
                    link_show.setImageResource(R.drawable.hide_pw);
                    pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pw.setSelection(pw.getText().length());
                    isShow = false;
                }else {
                    link_show.setImageResource(R.drawable.show_pw);
                    pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pw.setSelection(pw.getText().length());

                    isShow = true;
                }
                break;
            case R.id.linkItem_bt://确定关联 关联成功 登录
                accountS = account.getText().toString();
                pwS = pw.getText().toString();
                if (TextUtils.isEmpty(accountS)){
                    UCommonUtil.getUtil().showToast(this, "请输入账号");
                    return;
                }
                if (TextUtils.isEmpty(pwS)){
                    UCommonUtil.getUtil().showToast(this, "请输入密码");
                    return;
                }
                DialogUtils.getUtils().cancel();
                link(accountS, pwS);
                break;
        }
    }

    /**
     * 点击开始关联账户
     * */
    private ImageView link_close, link_show;
    private EditText account, pw;
    private Button add_link;
    private AlertDialog dialog;
    private boolean isShow = false;//设置的密码是明文还是隐藏 true 明文
    private String accountS, pwS;

    private void showLink(){
        View linkView = LayoutInflater.from(this).inflate(R.layout.view_dialog_link_account, null, false);
        link_close = (ImageView) linkView.findViewById(R.id.linkItem_close);
        link_close.setOnClickListener(this);
        account = (EditText) linkView.findViewById(R.id.linkItem_account);
        pw = (EditText) linkView.findViewById(R.id.linkItem_pw);
        link_show = (ImageView) linkView.findViewById(R.id.linkItem_show);
        link_show.setOnClickListener(this);

        add_link = (Button) linkView.findViewById(R.id.linkItem_bt);
        add_link.setOnClickListener(this);


        if (dialog == null || !dialog.isShowing()){
            dialog = DialogUtils.getUtils().initView(this, linkView, true, null);
        }
    }

    /**
     * 提交绑定账户
     * */
    private void link(String phoneS, String passWord){
        HttpMeManager.getPresenter().linkAccount(this, phoneS, passWord, new OnHttpResultSingleListener<LinkAccountBean>() {
            @Override
            public void onResult(int status, String msg, LinkAccountBean bean) {
                if (status ==0 && bean != null){
                    SwitchAccountUtils.getUtils().switchLink(LinkAccountActivity.this, bean);
//                    if (beanList == null){
//                        beanList = new ArrayList<LinkAccountBean>();
//                    }
//                    beanList.add(bean);
//                    adapter.setData(beanList);
                }else {
                    UCommonUtil.getUtil().showToast(LinkAccountActivity.this, msg);
                }
            }
        });
    }
}
